package poi;

import java.io.*;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.logging.*;


public class GeoInfoList extends LinkedList<GeoInfo> implements DataSource
{
	private static final long serialVersionUID = 5992093508109370783L;
	private static Logger log = Logger.getLogger("POISystem.GeoInfoList");
	
	@Override
	public boolean add(GeoInfo v){
		int k = getItemIndexByID(v.getPointID());
		if (k >= 0) {
			super.remove(k);
			super.add(k, v);
			log.info("POI \"" + v.getLocationName() + "\" updated.");
		} else {
			super.add(v);
			log.info("POI \"" + v.getLocationName() + "\" added.");
		}
		return true;
	}
	
	public int getNewID(){
		int max = 0;
		Iterator<GeoInfo> it = iterator();
		while (it.hasNext()) {
			GeoInfo val = it.next();
			if (val.getPointID() > max) {
				max = val.getPointID();
			} 
		}
		return ++max;		
	}
	
	public GeoInfoList()
	{
		super();
		LogFormatter.setLogFormat();
	}
	
	
	public GeoInfo getItemByID(int itemID)
	{
		Iterator<GeoInfo> it = iterator();
		while(it.hasNext())
		{
			GeoInfo val = it.next();
			if(val.getPointID()==itemID)
			{
				return val;				
			}
		}
		log.warning("POI with ID "+itemID+" not found.");
		return null;		
	}

	public int getItemIndexByID(int itemID)
	{
		Iterator<GeoInfo> it = iterator();
		int ind = 0;
		while(it.hasNext())
		{
			GeoInfo val = it.next();
			if(val.getPointID()==itemID)
			{
				return ind;				
			}
			ind++;
		}	
		log.warning("POI with ID "+itemID+" not found.");
		return -1;		
	}	
	
	public boolean connectSource(String fileName)
	{
		log.info("Loading \""+fileName+"\" file");
		if(super.size()!=0) super.clear();
        BufferedReader in = null;
        String fileContent = new String();
        try {
            in = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
            while (in.ready()) {
            	fileContent = in.readLine();
        		String fileData[] = fileContent.split(",");
        		if(fileData.length!=5)
        		{
        			log.severe("CSV file formt wrong.");
        			throw new IndexOutOfBoundsException("Some lines in CSV file containe more or less than 5 fields!");
        		};
				GeoInfo info;
				info=new GeoInfo(Integer.parseInt(fileData[0]),fileData[1],fileData[2],Double.parseDouble(fileData[3]+"f"),Double.parseDouble(fileData[4]+"f"));
				super.add(info);
           }
        } catch (IOException e) {
        	log.severe("Can't open \""+fileName+"\" file for read.");
        	return false;
        }
        finally
        {
        	try {
				in.close();
			} catch (IOException e) {
	        	log.severe("Can't close \""+fileName+"\" file.");
				return false;
			}        	
        }
        log.info("File successfully loaded.");
        return true;
	}
	
	public boolean postData(String fileName)
	{
		log.info("Writing \""+fileName+"\" file");
        BufferedWriter out = null;
            try {
				out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName)));
				Iterator<GeoInfo> it = iterator();
				while(it.hasNext())
				{
					GeoInfo val = it.next();
					out.write(val.toString());
					if(it.hasNext())out.newLine();
				}
			} catch (IOException e) {
	        	log.severe("Can't create \""+fileName+"\" file.");
				return false;
			}
			finally {
				try {
					out.close();
				} catch (IOException e) {
		        	log.severe("Can't close \""+fileName+"\" file.");
					return false;
				}
			}
	        log.info("File successfully saved.");
			return true;
	}
	
	protected void finalize() {
		log.info("POISearch object deleted.");
	}	
	
}